//
//  LoginViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/17.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "AFNetworking.h"

#import "UIButton+CountDown.h"

#import "TTUDIDManager.h"
#import "NSString+Hash.h"

#import "LoginHandleModel.h"

#import "PhoneSmsLoginView.h"
#import "PhonePswLoginView.h"
#import "SmsCodeInputView.h"
#import "BottomExchangeView.h"

#import "LoginViewController.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *loginBGImageView;


@property (weak, nonatomic) IBOutlet UIView *inputBGView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputHeight;

@property (weak, nonatomic) IBOutlet UIView *exchangeBGView;


@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@property (weak, nonatomic) IBOutlet UIView *privacyBGView;

@property (nonatomic, strong) LoginHandleModel *handleModel;


@property (nonatomic, strong) PhoneSmsLoginView *PhoneSmsView;
@property (nonatomic, strong) PhonePswLoginView *PhonePswView;
@property (nonatomic, strong) SmsCodeInputView *SmsCodeView;
@property (nonatomic, strong) BottomExchangeView *ExchangeView;

@property (nonatomic, strong) NSMutableArray *stepArray;


@end

@implementation LoginViewController

- (instancetype)init{
    
    if (self == [super init]) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.type = TT_LoginType_PassWord;
    [self prepareData];
    [self initView];
}

- (void)prepareData {
    self.handleModel = [[LoginHandleModel alloc] init];
    self.handleModel.alias = [TTUDIDManager getUniqueDeviceID];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"BA_marryOauth" object:nil];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)initView {
    
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    [self configPhonePswView];
    [self configExchangeView];
    [self.loginBGImageView setImage:[UIImage imageNamed:@"login_banner1"]];

WEAKSELF
    
    self.PhonePswView.PhonePswLoginViewBlock = ^(PhonePswActionType type) {
        
        switch (type) {
            case PhonePswActionType_login:{
                [weakSelf request_login];
            } break;
                
            default:
                break;
        }
    };
    
    
    [[RACSignal merge:@[self.PhonePswView.phoneTF.rac_textSignal, RACObserve(self.PhonePswView.phoneTF, text)]] subscribeNext:^(NSString* text){
        self.handleModel.mobile = text;
    }];
    
    [[RACSignal merge:@[self.PhonePswView.pswTF.rac_textSignal, RACObserve(self.PhonePswView.pswTF, text)]] subscribeNext:^(NSString* text){
        self.handleModel.passWord = text;
    }];
    
    [[RACSignal merge:@[self.PhoneSmsView.phoneTF.rac_textSignal, RACObserve(self.PhonePswView.phoneTF, text)]] subscribeNext:^(NSString* text){
        self.handleModel.mobile = text;
    }];
}

- (void)clearView {
    
    [self.PhonePswView removeFromSuperview];
    [self.PhoneSmsView removeFromSuperview];
    [self.SmsCodeView removeFromSuperview];
}

- (void)configPhonePswView {
    [self clearView];
    
    [self.inputBGView addSubview:self.PhonePswView];
    [self.PhonePswView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    self.inputHeight.constant = [PhonePswLoginView calculateHeight];
    if (![self.stepArray containsObject:@(TT_LoginStepType_phonePsw)]) {
        [self.stepArray addObject:@(TT_LoginStepType_phonePsw)];
    }
    self.type = TT_LoginType_PassWord;
    self.titleLab.text = @"账号密码登录";
    [self.ExchangeView.exchangeBtn setTitle:@"手机号快速登录" forState:UIControlStateNormal];
    self.PhonePswView.phoneTF.text = self.handleModel.mobile;
    self.ExchangeView.hidden = NO;
}

- (void)configPhoneSmsView {
    [self clearView];
    
    [self.inputBGView addSubview:self.PhoneSmsView];
    [self.PhoneSmsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    self.inputHeight.constant = [PhoneSmsLoginView calculateHeight];
    
    if (![self.stepArray containsObject:@(TT_LoginStepType_phoneSms)]) {
        [self.stepArray addObject:@(TT_LoginStepType_phoneSms)];
    }
    
    self.type = TT_LoginType_Code;
    self.titleLab.text = @"手机号快速登录";
    [self.ExchangeView.exchangeBtn setTitle:@"账号密码登录" forState:UIControlStateNormal];
    self.PhoneSmsView.phoneTF.text = self.handleModel.mobile;
    self.ExchangeView.hidden = NO;
}

- (void)configExchangeView {
    
    [self.exchangeBGView addSubview:self.ExchangeView];
    [self.ExchangeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
}

#pragma mark 返回按钮
- (IBAction)closeAction:(id)sender {
    
        //先判断步骤数组里面最后一条数据
    [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    [[TT_TabbarViewController shareTabbar] setCurrentIndex:TT_TabbarIndexType_Home];
    
}

- (PhoneSmsLoginView *)PhoneSmsView {
    if (!_PhoneSmsView) {
        _PhoneSmsView = [[NSBundle mainBundle] loadNibNamed:@"PhoneSmsLoginView"owner:nil options:nil].firstObject;
    }
    return _PhoneSmsView;
}

- (PhonePswLoginView *)PhonePswView {
    if (!_PhonePswView) {
        _PhonePswView = [[NSBundle mainBundle] loadNibNamed:@"PhonePswLoginView"owner:nil options:nil].firstObject;
    }
    return _PhonePswView;
}

- (SmsCodeInputView *)SmsCodeView {
    if (!_SmsCodeView) {
        _SmsCodeView = [[NSBundle mainBundle] loadNibNamed:@"SmsCodeInputView"owner:nil options:nil].firstObject;
    }
    return _SmsCodeView;
}

- (BottomExchangeView *)ExchangeView {
    if (!_ExchangeView) {
        _ExchangeView = [[NSBundle mainBundle] loadNibNamed:@"BottomExchangeView"owner:nil options:nil].firstObject;
    }
    return _ExchangeView;
}

- (NSMutableArray *)stepArray {
    if (!_stepArray) {
        _stepArray = @[].mutableCopy;
    }
    return _stepArray;
}

#pragma mark 账号密码登录login
- (void)request_login {

    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.handleModel.mobile forKey:@"phoneNo"];
    [dic setValue:self.handleModel.passWord forKey:@"passWord"];
    [dic setValue:@"PRA-AL00X" forKey:@"modelName"];
    [dic setValue:@"android" forKey:@"osType"];
    [dic setValue:@"201" forKey:@"versionNo"];
    [dic setValue:@"862859039636168" forKey:@"deviceId"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",BA_Global_Domain,API_Login];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 10.0;
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
    
    [manager POST:url parameters:dic headers:@{} progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];

            NSLog(@"%@",jsonDict);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        }];
    
    
}

@end
